package br.com.futibamanager.bean;

import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 * The Message Managed Bean.
 * 
 * @author diogenes.feijo
 *
 */
public class MessageMB {

	/**
	 * Verifies if there are info messages present in the JSF context.
	 * 
	 * @return boolean
	 */
	public boolean getInfoMessagesPresence() {
		
		return areThereMessagesPresent(FacesMessage.SEVERITY_INFO);
	}
	
	/**
	 * Verifies if there are error messages present in the JSF context.
	 * 
	 * @return boolean
	 */
	public boolean getErrorMessagesPresence() {
		
		return areThereMessagesPresent(FacesMessage.SEVERITY_ERROR);
	}
	
	/*
	 * Verifies if there are messages present in the JSF context.
	 * 
	 * @param severity - the desired message severity to verify
	 * 
	 * @return boolean
	 */
	private boolean areThereMessagesPresent(FacesMessage.Severity severity) {

		Iterator<FacesMessage> messagesIterator = 
			FacesContext.getCurrentInstance().getMessages();
		
		while (messagesIterator.hasNext()) {
			FacesMessage message = messagesIterator.next();
			
			if (message.getSeverity().equals(severity)) {
				return true;
			}
		}
		
		return false;
	}

}
